/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fri.cbw.MyTestPlotTool1;

import fri.cbw.CBWutil.InboundConnectionException;
import fri.cbw.GenericTool.AbstractEngineTool;
import fri.cbw.GenericTool.AbstractGenericTool;
import fri.cbw.GenericTool.ToolTopComponent;
import java.awt.BorderLayout;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.chart.Chart;
import javafx.scene.chart.XYChart;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;

/**
 * Top component which displays something.
 */
@TopComponent.Description(
    preferredID = "MyTestPlotToolTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_NEVER)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "fri.cbw.MyTestPlotTool1.MyTestPlotToolTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@Messages({
    "CTL_MyTestPlotToolAction=MyTestPlotTool",
    "CTL_MyTestPlotToolTopComponent=MyTestPlotTool Window",
    "HINT_MyTestPlotToolTopComponent=This is a MyTestPlotTool window"
})
public final class MyTestPlotToolTopComponent extends ToolTopComponent {
    
    private static JFXPanel chartFxPanel;
    
    private Chart chart;
    //private Pane browser;
    
    
    public MyTestPlotToolTopComponent(AbstractGenericTool gt) {
        super(gt);
        initComponents();
        setName(Bundle.CTL_MyTestPlotToolTopComponent());
        setToolTipText(Bundle.HINT_MyTestPlotToolTopComponent());
        initLineChart();
    }
    
    public void initLineChart() {
        // create javafx panel for charts
        chartFxPanel = new JFXPanel();
        jPanelGraph.add(chartFxPanel, BorderLayout.CENTER);
    }
    
    
    private void createChartScene(boolean calculate) {
        try {
            Platform.setImplicitExit(false);
            AbstractEngineTool gt = (AbstractEngineTool) getGenericTool().getFirstInboundModul();
                
            /* če izvedemo ponovni izračun */
            if(calculate)
                gt.calculate();
            
            chart = gt.getLineChartData();
            if(chart != null)
                chartFxPanel.setScene(new Scene(chart));
        } catch (InboundConnectionException ex) {
            Exceptions.printStackTrace(ex);
        }
        
    }
    
    
    @Override
    public void componentOpened() {
        /*
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                createChartScene(false);
            }
        });
        */
    }

    @Override
    public void componentClosed() {
        //Platform.exit();
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelGraph = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        btnRefresh = new javax.swing.JButton();
        btnLinear = new javax.swing.JButton();
        btnBar = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanelGraph.setLayout(new java.awt.BorderLayout());

        org.openide.awt.Mnemonics.setLocalizedText(btnRefresh, org.openide.util.NbBundle.getMessage(MyTestPlotToolTopComponent.class, "MyTestPlotToolTopComponent.btnRefresh.text")); // NOI18N
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnLinear, org.openide.util.NbBundle.getMessage(MyTestPlotToolTopComponent.class, "MyTestPlotToolTopComponent.btnLinear.text")); // NOI18N
        btnLinear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLinearActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnBar, org.openide.util.NbBundle.getMessage(MyTestPlotToolTopComponent.class, "MyTestPlotToolTopComponent.btnBar.text")); // NOI18N
        btnBar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnRefresh)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnLinear)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnBar)
                .addContainerGap(110, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnRefresh)
                    .addComponent(btnLinear)
                    .addComponent(btnBar))
                .addContainerGap())
        );

        jPanelGraph.add(jPanel1, java.awt.BorderLayout.NORTH);

        add(jPanelGraph, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        Platform.runLater(new Runnable() {
            public void run() {
                createChartScene(true);
            }
        });
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void btnLinearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLinearActionPerformed
        Platform.runLater(new Runnable() {
            public void run() {
                Platform.setImplicitExit(false);
                chart = DemoCharts.demoLinearCharts();
                chartFxPanel.setScene(new Scene(chart)); 
            }
        });
    }//GEN-LAST:event_btnLinearActionPerformed

    private void btnBarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBarActionPerformed
        Platform.runLater(new Runnable() {
            public void run() {
                Platform.setImplicitExit(false);
                chart = DemoCharts.demoBarChart();
                chartFxPanel.setScene(new Scene(chart));
            }
        });
    }//GEN-LAST:event_btnBarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBar;
    private javax.swing.JButton btnLinear;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanelGraph;
    // End of variables declaration//GEN-END:variables
    
    
    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    public void doSave() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
